// 方法2：通过Adapter直接访问TextView

class YourAdapter : RecyclerView.Adapter<YourAdapter.YourViewHolder>() {
    private val dataList = mutableListOf<YourData>()
    private val viewHolders = mutableMapOf<Int, YourViewHolder>()
    
    class YourViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val textView: TextView = itemView.findViewById(R.id.your_text_view_id)
        val imageView: ImageView = itemView.findViewById(R.id.your_image_view_id)
        // 其他控件...
    }
    
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): YourViewHolder {
        val view = LayoutInflater.from(parent.context)
            .inflate(R.layout.your_item_layout, parent, false)
        return YourViewHolder(view)
    }
    
    override fun onBindViewHolder(holder: YourViewHolder, position: Int) {
        // 保存ViewHolder引用
        viewHolders[position] = holder
        
        // 绑定数据
        val item = dataList[position]
        holder.textView.text = item.title
        // 其他绑定逻辑...
    }
    
    override fun onViewRecycled(holder: YourViewHolder) {
        super.onViewRecycled(holder)
        // 清理ViewHolder引用
        val position = holder.adapterPosition
        if (position != RecyclerView.NO_POSITION) {
            viewHolders.remove(position)
        }
    }
    
    override fun getItemCount(): Int = dataList.size
    
    // 获取特定位置的TextView
    fun getTextViewAtPosition(position: Int): TextView? {
        return viewHolders[position]?.textView
    }
    
    // 获取所有可见的TextView
    fun getAllVisibleTextViews(): List<TextView> {
        return viewHolders.values.mapNotNull { it.textView }
    }
    
    // 更新特定位置的TextView
    fun updateTextViewAtPosition(position: Int, newText: String) {
        viewHolders[position]?.textView?.text = newText
    }
    
    // 设置数据
    fun setData(newData: List<YourData>) {
        dataList.clear()
        dataList.addAll(newData)
        viewHolders.clear() // 清理旧的ViewHolder引用
        notifyDataSetChanged()
    }
}

// 在Activity中使用
class YourActivity : AppCompatActivity() {
    private lateinit var adapter: YourAdapter
    
    fun exampleUsage() {
        // 获取第3个item的TextView
        val textView = adapter.getTextViewAtPosition(3)
        textView?.let {
            it.text = "更新的文本"
            it.setTextColor(Color.BLUE)
        }
        
        // 更新特定位置的文本
        adapter.updateTextViewAtPosition(5, "新的文本内容")
        
        // 获取所有可见的TextView
        val allTextViews = adapter.getAllVisibleTextViews()
        allTextViews.forEach { textView ->
            textView.setTextColor(Color.RED)
        }
    }
} 